<?php

namespace App\Admin\Controllers;

use App\Models\WechatUser;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class WechatUserController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new WechatUser(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('openid', 'OpenID')->copyable();
            $grid->column('unionid', 'UnionID')->copyable();
            $grid->column('nickname', '昵称');
            $grid->column('avatar', '头像')->image('', 50, 50);
            $grid->column('gender_text', '性别');
            $grid->column('full_address', '地区');
            $grid->column('phone', '手机号')->copyable();
            $grid->column('email', '邮箱')->copyable();
            $grid->column('subscribe_status_text', '关注状态')->label([
                '已关注' => 'success',
                '未关注' => 'danger',
            ]);
            $grid->column('subscribe_time', '关注时间');
            $grid->column('created_at', '创建时间');
            $grid->column('updated_at', '更新时间')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
                $filter->like('openid', 'OpenID');
                $filter->like('unionid', 'UnionID');
                $filter->like('nickname', '昵称');
                $filter->like('phone', '手机号');
                $filter->like('email', '邮箱');
                $filter->equal('gender', '性别')->select([
                    WechatUser::GENDER_UNKNOWN => '未知',
                    WechatUser::GENDER_MALE => '男',
                    WechatUser::GENDER_FEMALE => '女',
                ]);
                $filter->equal('subscribe_status', '关注状态')->select([
                    WechatUser::SUBSCRIBE_STATUS_SUBSCRIBED => '已关注',
                    WechatUser::SUBSCRIBE_STATUS_UNSUBSCRIBED => '未关注',
                ]);
                $filter->like('country', '国家');
                $filter->like('province', '省份');
                $filter->like('city', '城市');
                $filter->between('subscribe_time', '关注时间')->datetime();
                $filter->between('created_at', '创建时间')->datetime();
            });

            $grid->quickSearch(['nickname', 'openid', 'unionid', 'phone', 'email']);

            $grid->export();
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new WechatUser(), function (Show $show) {
            $show->field('id');
            $show->field('openid', 'OpenID');
            $show->field('unionid', 'UnionID');
            $show->field('nickname', '昵称');
            $show->field('avatar', '头像')->image();
            $show->field('gender_text', '性别');
            $show->field('country', '国家');
            $show->field('province', '省份');
            $show->field('city', '城市');
            $show->field('language', '语言');
            $show->field('phone', '手机号');
            $show->field('email', '邮箱');
            $show->field('subscribe_status_text', '关注状态');
            $show->field('subscribe_time', '关注时间');
            $show->field('unsubscribe_time', '取消关注时间');
            $show->field('remark', '备注');
            $show->field('privileges', '特权信息')->json();
            $show->field('created_at', '创建时间');
            $show->field('updated_at', '更新时间');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new WechatUser(), function (Form $form) {
            $form->display('id');
            $form->text('openid', 'OpenID')->required()->help('微信用户唯一标识');
            $form->text('unionid', 'UnionID')->help('微信开放平台唯一标识');
            $form->text('nickname', '昵称')->required();
            $form->image('avatar', '头像')->help('用户头像图片地址');
            $form->radio('gender', '性别')->options([
                WechatUser::GENDER_UNKNOWN => '未知',
                WechatUser::GENDER_MALE => '男',
                WechatUser::GENDER_FEMALE => '女',
            ])->default(WechatUser::GENDER_UNKNOWN);
            $form->text('country', '国家')->default('中国');
            $form->text('province', '省份');
            $form->text('city', '城市');
            $form->text('language', '语言')->default('zh_CN');
            $form->mobile('phone', '手机号');
            $form->email('email', '邮箱');
            $form->datetime('subscribe_time', '关注时间');
            $form->datetime('unsubscribe_time', '取消关注时间');
            $form->radio('subscribe_status', '关注状态')->options([
                WechatUser::SUBSCRIBE_STATUS_SUBSCRIBED => '已关注',
                WechatUser::SUBSCRIBE_STATUS_UNSUBSCRIBED => '未关注',
            ])->default(WechatUser::SUBSCRIBE_STATUS_SUBSCRIBED);
            $form->textarea('remark', '备注');
            $form->tags('privileges', '特权信息')->help('用户特权信息，多个用逗号分隔');

            $form->display('created_at', '创建时间');
            $form->display('updated_at', '更新时间');
        });
    }
}